package com.stray.crawl.worker;

import java.net.URL;

import com.stray.crawl.utils.DataUtils;

public final class HttpEnums {
	
	public interface HttpHeaderKey {
		
		String HOST = "Host";
		String ACCEPT = "Accept";
		String ACCEPT_LANGUAGE = "Accept-Language";
		String CONNECTION = "Connection";
		String REFERER = "Referer";
		String CACHE_CONTROL = "Cache-Control";
		String IF_MODIFIED_SINCE = "If-Modified-Since";
		String IF_NONE_MATCH = "If-None-Match";
		String PRAGMA = "Pragma";
		
	}
	
	public interface HttpHeaderValue {
		
		String NO_CACHE = "no-cache";
		String CONNECTION_KEY_KEEP_ALIVE = "keep-alive";
		String ACCEPT_LANGUAGE_SUFFIX = ";q=0.7,en;q=0.3";
		
	}
	
	public enum RequestMethod {
		
		get, post
		
	}
	
	public enum AcceptHeader {
		
		html(
			new String[] {"htm", "html" , "aspx", "jsf"}, 
			"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
		), 
		image(
			new String[] {"png", "jpg" , "jpeg", "gif"}, 
			"image/png,image/*;q=0.8,*/*;q=0.5"
		),
		resource(
			new String[] {"css", "js" , "txt"},
			"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
		);
		
		String[] extensions;
		String value;

		AcceptHeader(String[] extensions, String value) {
			this.extensions = DataUtils.copyStringArray(extensions);
			this.value = value;
		}

		public String getValue() {
			return value;
		}
		
	}
	
	public static RequestMethod getRequestMethod(String method) { 
		RequestMethod[] methods = RequestMethod.values();
		
		for (RequestMethod reqMethod : methods) {
			if (reqMethod.name().equals(method)) {
				return reqMethod;
			}
		}
		
		return methods[0];
	}
	
	public static String getHostHeader(URL url) {
		return url.getPort() > 0 ? url.getHost() + ":" + url.getPort() : url.getHost();
	}
	
	public static AcceptHeader getAcceptHeader(URL url) {
		String fileExt = getFileExtension(url);
		AcceptHeader[] headers = AcceptHeader.values();
		
		for (AcceptHeader header : headers) {
			for (String ext : header.extensions) {
				if (ext.equals(fileExt)) {
					return header;
				}
			}
			
		}
		
		return headers[0];
	}
	
	public static String getFilename(URL url) {
		String filename = "";
		String path = url.getPath();

		String[] pathContents = path.split("[\\\\/]");
		if (pathContents != null) {
			int pathContentsLength = pathContents.length;
			
			filename = pathContents[pathContentsLength - 1];
		}
		
		return filename;
	}
	
	public static String getFileExtension(URL url) {
		String extension = "";
		String path = url.getPath();

		String[] pathContents = path.split("[\\\\/]");
		if (pathContents != null) {
			int pathContentsLength = pathContents.length;
			
			String lastPart = pathContents[pathContentsLength - 1];
			String[] lastPartContents = lastPart.split("\\.");
			
			if (lastPartContents != null && lastPartContents.length > 1) {
				int lastPartContentLength = lastPartContents.length;
				extension = lastPartContents[lastPartContentLength - 1];
			}
		}
		
		return extension;
	}
	
}